﻿
@{
    Layout = null;
}

<div>
    <div class="input-group">
        <div class="col-3 pr-0" style="display: none">
            <label id="SAMPLE-AddSpecCollection-SpecId">@ViewData["SpecId"]</label>
            <label id="SAMPLE-AddSpecCollection-Parent-ModalID">@ViewData["ModalID"]</label>
        </div>
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">代码*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddSpecCollection-Code" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">名称*</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddSpecCollection-Name" class="form-control font-rfs border-radius-noleft" placeholder="必填" />
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">一级分类*</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-AddSpecCollection-Select-FirstGroup" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            <div class="col-11 pl-0 input-group position-absolute float-left"><input id="SAMPLE-AddSpecCollection-Input-FirstGroup" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" /></div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">二级分类</span>
        </div>
        <div class="col-9 pl-0 input-group position-relative">
            <select id="SAMPLE-AddSpecCollection-Select-SecondGroup" class="form-control font-rfs border-radius-noleft" style="width:90%"></select>
            <div class="col-11 pl-0 input-group position-absolute float-left"><input id="SAMPLE-AddSpecCollection-Input-SecondGroup" class="form-control font-rfs border-radius-noleft noRightBorder" style="width:100%" placeholder="可输入新的类型" /></div>
        </div>
    </div>
    <div class="input-group">
        <div class="col-3 pr-0">
            <span class="input-group-text font-rfs border-radius-noright noRightBorder">描述</span>
        </div>
        <div class="col-9 pl-0">
            <input id="SAMPLE-AddSpecCollection-Description" class="form-control font-rfs border-radius-noleft" placeholder="可选" />
        </div>
    </div>
    <br />
    <div class="text-right">
        <button id="Supplier-AddSpecCollection-confirm" class="btn btn-primary font-rfs">确定</button>
        <button id="Supplier-AddSpecCollection-cancel" type="button" class="btn btn-info font-rfs">取消</button>
    </div>
</div>

<script>
    var AddSpecCollection_specId = $("#SAMPLE-AddSpecCollection-SpecId").html();

    /*初始化页面控件值，加载一级类别*/
    $(document).ready(function() { 

        LoadSpecCollectionFirstGroups();

        if (AddSpecCollection_specId.length > 0) {
            // 如果是编辑状态，初始化页面控件值
            $.post("GetSpec", {
                Id: AddSpecCollection_specId
            }, function (data) {
                $("#SAMPLE-AddSpecCollection-Code").val(data.code);  /*这个地方属性首字母都是小写，否则数据绑定不上*/
                $("#SAMPLE-AddSpecCollection-Name").val(data.name);
                $("#SAMPLE-AddSpecCollection-Input-FirstGroup").val(data.firstGroup);
                $("#SAMPLE-AddSpecCollection-Input-SecondGroup").val(data.secondGroup);
                $("#SAMPLE-AddSpecCollection-Description").val(data.description);
            });
        }
    });

    /*加载一级类别*/
    function LoadSpecCollectionFirstGroups() {
        $("#SAMPLE-AddSpecCollection-Select-FirstGroup").empty();
        $.post("GetSpecCollectionFirstGroup", {
            //Type: type  // 传递的参数，对应控制器里的参数
        }, function (data) {
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddSpecCollection-Select-FirstGroup").append(new Option(data[i].firstGroup));
            }
            $("#SAMPLE-AddSpecCollection-Select-FirstGroup").prepend(new Option("全部"));
            $("#SAMPLE-AddSpecCollection-Select-FirstGroup").prepend(new Option("", ""));
            $("#SAMPLE-AddSpecCollection-Select-FirstGroup").val("");
        });
    }

    $("#SAMPLE-AddSpecCollection-Select-FirstGroup").change(function () {
        $("#SAMPLE-AddSpecCollection-Input-FirstGroup").val($("#SAMPLE-AddSpecCollection-Select-FirstGroup").find("option:selected").text());
        $("#SAMPLE-AddSpecCollection-Input-SecondGroup").val('');

        $.post("GetSpecCollectionSecondGroup", { /* 加载 二级分类 信息*/
            FirstGroup: $("#SAMPLE-AddSpecCollection-Select-FirstGroup").find("option:selected").text()  // 传递的参数，对应控制器里的参数
        }, function (data) {
            $("#SAMPLE-AddSpecCollection-Select-SecondGroup").empty();
            for (var i = 0; i < data.length; i++) {
                $("#SAMPLE-AddSpecCollection-Select-SecondGroup").append(new Option(data[i].secondGroup));
            }
            $("#SAMPLE-AddSpecCollection-Select-SecondGroup").prepend(new Option("", ""));
            $("#SAMPLE-AddSpecCollection-Select-SecondGroup").val("");
        });
    });

    $("#SAMPLE-AddSpecCollection-Select-SecondGroup").change(function () {
        $("#SAMPLE-AddSpecCollection-Input-SecondGroup").val($("#SAMPLE-AddSpecCollection-Select-SecondGroup").find("option:selected").text());
    });

    $("#Supplier-AddSpecCollection-confirm").click(function () {
        if ($.trim($("#SAMPLE-AddSpecCollection-Code").val()).length == 0) {
            alert("指标集代码不能为空！");
            $("#SAMPLE-AddSpecCollection-Code").addClass('error-style');
            return false;
        }
        if ($.trim($("#SAMPLE-AddSpecCollection-Name").val()).length == 0) {
            alert("指标集名称不能为空！");
            $("#SAMPLE-AddSpecCollection-Name").addClass('error-style');
            return false;
        }
        if ($.trim($("#SAMPLE-AddSpecCollection-Input-FirstGroup").val()).length == 0) {
            alert("指标集一级类型不能为空！");
            $("#SAMPLE-AddSpecCollection-Input-FirstGroup").addClass('error-style');
            return false;
        }

        var jsonStr = {};
        jsonStr["Code"] = $.trim($("#SAMPLE-AddSpecCollection-Code").val());
        jsonStr["Name"] = $.trim($("#SAMPLE-AddSpecCollection-Name").val());
        jsonStr["FirstGroup"] = $.trim($("#SAMPLE-AddSpecCollection-Input-FirstGroup").val());
        jsonStr["SecondGroup"] = $.trim($("#SAMPLE-AddSpecCollection-Input-SecondGroup").val());
        jsonStr["Description"] = $.trim($("#SAMPLE-AddSpecCollection-Description").val());

        if (AddSpecCollection_specId == '' || AddSpecCollection_specId == null || AddSpecCollection_specId == undefined) {
            $.post("postAddSampleSpecCollection", {
                jsonStr: JSON.stringify(jsonStr)
            }, function(data) {
                if (data.indexOf("成功") >= 0) {
                    var modalID = $("#SAMPLE-AddSpecCollection-Parent-ModalID").html(); // 这两行代码不能提到最后面，否则因为异步会造成刷新不到数据。
                    $("#" + modalID).modal('hide');
                } else {
                    alert(data);
                }
            });
        } else {
            jsonStr["Id"] = AddSpecCollection_specId;
            $.post("postUpdateSampleSpecCollection", {
                jsonStr: JSON.stringify(jsonStr)
            }, function(data) {
                if (data.indexOf("成功") >= 0) {
                    var modalID = $("#SAMPLE-AddSpecCollection-Parent-ModalID").html(); // 这两行代码不能提到最后面，否则因为异步会造成刷新不到数据。
                    $("#" + modalID).modal('hide');
                } else {
                    alert(data);
                }
            });
        }

    });

    $("#Supplier-AddSpecCollection-cancel").click(function () {
        var modalID = $("#SAMPLE-AddSpecCollection-Parent-ModalID").html();
        $("#" + modalID).modal('hide');
    });
    
</script>
